﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hfm.Common.Entity.CustomerStock;
using System.Data.SqlClient;
using System.Transactions;

namespace Hfm.Core.DataAccess.CustomerStock
{
    public class CustomerStockData
    {
        protected readonly CoreContext Context;
        public CustomerStockData()
        {

        }

        public CustomerStockData(CoreContext coreContext)
        {
            Context = coreContext;
        }

        /// <summary>
        /// 创建CustomerStock
        /// </summary>
        /// <param name="CustomerStock"></param>
        public virtual void CreateCustomerStock(CustomerStockEntity CustomerStock)
        {
            Context.CustomerStockEntities.Add(CustomerStock);
            Context.SaveChanges();
        }

        /// <summary>
        /// 创建StockInventory
        /// </summary>
        /// <param name="StockInventory"></param>
        public virtual void CreateStockInventory(CustomerStockInventoryEntity StockInventory)
        {
            Context.CustomerStockInventoryEntities.Add(StockInventory);
            Context.SaveChanges();
        }

        /// <summary>
        /// 创建StockSerialNumber
        /// </summary>
        /// <param name="StockSerialNumber"></param>
        public virtual void CreateStockSerialNumber(CustomerStockSerialNumberEntity StockSerialNumber)
        {
            Context.CustomerStockSercialNumberEntities.Add(StockSerialNumber);
            Context.SaveChanges();
        }

        /// <summary>
        /// 创建StockEmailRecipient
        /// </summary>
        /// <param name="StockEmailRecipient"></param>
        public virtual void CreateStockEmailRecipient(CustomerStockEmailRecipientEntity StockEmailRecipient)
        {
            Context.CustomerStockEmailRecipientEntities.Add(StockEmailRecipient);
            Context.SaveChanges();
        }

        /// <summary>
        /// 获取所有CustomerStock
        /// </summary>
        /// <returns></returns>
        public virtual IList<CustomerStockEntity> GetCustomerStocks()
        {
            var query = from c in Context.CustomerStockEntities
                        select c;
            return query.ToList();
        }

        /// <summary>
        /// 根据条件获取CustomerStock
        /// </summary>
        /// <param name="StockId"></param>
        /// <returns></returns>
        public virtual CustomerStockEntity getCustomerStockByStockId(int StockId)
        {
            var query = from cs in Context.CustomerStockEntities
                        where cs.Id == StockId
                        select cs;
            return query.SingleOrDefault();
        }

        /// <summary>
        /// 根据条件获取StockInventory
        /// </summary>
        /// <param name="StockId">StockId</param>
        /// <returns></returns>
        public virtual IList<CustomerStockInventoryEntity> GetCustomerStockInventoryByStockId(int StockId)
        {
            var query = from csi in Context.CustomerStockInventoryEntities
                        where csi.StockId == StockId
                        select csi;
            return query.ToList();
        }

        /// <summary>
        /// 根据条件获取CustomerStock
        /// </summary>
        /// <param name="CustomerId"></param>
        /// <param name="ItemId"></param>
        /// <returns></returns>
        public virtual CustomerStockEntity GetCustomerStockByCustomerIdAndItemId(int CustomerId, int ItemId)
        {
            var query = from csi in Context.CustomerStockEntities
                        where csi.CustomerId == CustomerId && csi.ItemId == ItemId
                        select csi;
            return query.SingleOrDefault();
        }

        /// <summary>
        /// 获取所有StockEmailRecipient
        /// </summary>
        /// <returns></returns>
        public virtual List<CustomerStockEmailRecipientEntity> GetStockEmailRecipients()
        {
            var query = from er in Context.CustomerStockEmailRecipientEntities
                        select er;
            return query.ToList();
        }

        /// <summary>
        /// 根据条件获取StockEmailRecipient
        /// </summary>
        /// <param name="StockId"></param>
        /// <returns></returns>
        public virtual List<CustomerStockEmailRecipientEntity> GetStockEmailRecipientByUserId(int UserId)
        {
            var query = from er in Context.CustomerStockEmailRecipientEntities
                        where er.UserId == UserId
                        select er;
            return query.ToList();
        }

        /// <summary>
        /// 根据条件获取StockSerialNumber
        /// </summary>
        /// <param name="StockId">StockId</param>
        /// <param name="SerialNumber">SerialNumber</param>
        /// <returns></returns>
        public virtual List<CustomerStockSerialNumberEntity> GetCustomerStockSerialNumberByStockId(int StockId, string SerialNumber = null)
        {
            var query = from cssn in Context.CustomerStockSercialNumberEntities
                        where cssn.StockId == StockId
                        select cssn;

            if (!string.IsNullOrWhiteSpace(SerialNumber))
            {
                query = query.Where(cssn => cssn.SerialNumber.Contains(SerialNumber));
            }
            return query.ToList();
        }

        /// <summary>
        /// 更新CustomerStock
        /// </summary>
        /// <param name="CustomerStock"></param>
        public virtual void UpdateCustomerStock(CustomerStockEntity CustomerStock)
        {
            Context.CustomerStockEntities.Attach(CustomerStock);
            Context.Entry(CustomerStock).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        /// <summary>
        /// 更新Stock Email Recipient
        /// </summary>
        /// <param name="StockEmailRecipient"></param>
        public virtual void UpdateStockEmailRecipient(CustomerStockEmailRecipientEntity StockEmailRecipient)
        {
            Context.CustomerStockEmailRecipientEntities.Attach(StockEmailRecipient);
            Context.Entry(StockEmailRecipient).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        /// <summary>
        /// 删除CustomerStock
        /// </summary>
        /// <param name="CustomerStockId"></param>
        public virtual void DeleteCustomerStock(int CustomerStockId)
        {
            string strSql = "DELETE FROM CustomerStock WHERE Id = @Id ";
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Id", CustomerStockId));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());

            strSql = "DELETE FROM CustomerStockInventory WHERE StockId = @Id ";
            param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Id", CustomerStockId));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());

            strSql = "DELETE FROM CustomerStockSerialNumber WHERE StockId = @Id ";
            param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Id", CustomerStockId));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());

            strSql = "DELETE FROM CustomerStockEmailRecipient WHERE StockId = @Id ";
            param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Id", CustomerStockId));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());
        }

        /// <summary>
        /// 删除StockInventory
        /// </summary>
        /// <param name="StockId"></param>
        public virtual void DeleteStockInventory(int StockId)
        {
            string strSql = "DELETE FROM CustomerStockInventory WHERE StockId = @Id ";
            List<SqlParameter> param = new List<SqlParameter>();
            param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Id", StockId));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());
        }

        /// <summary>
        /// 删除StockSerialNumber
        /// </summary>
        /// <param name="StockId"></param>
        public virtual void DeletStockSerialNumber(int StockId)
        {
            string strSql = "DELETE FROM CustomerStockSerialNumber WHERE StockId = @Id ";
            List<SqlParameter> param = new List<SqlParameter>();
            param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Id", StockId));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());
        }

        /// <summary>
        /// 删除StockEmailRecipient
        /// </summary>
        /// <param name="StockId"></param>
        public virtual void DeleteStockEmailRecipient(int StockEmailId)
        {
            string strSql = "DELETE FROM CustomerStockEmailRecipient WHERE Id = @Id ";
            List<SqlParameter> param = new List<SqlParameter>();
            param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Id", StockEmailId));
            Context.Database.ExecuteSqlCommand(strSql, param.ToArray());
        }
    }
}
